package site.isscloud.jt809;

import java.io.*;

public class DriverInfo {
    private String name;               // 姓名(16字节)
    private String idCard;            // 身份证号(18字节)
    private String qualificationNo;   // 从业资格证号(20字节)
    private String company;           // 所属企业(50字节)
    
    public DriverInfo(String name, String idCard, String qualificationNo, String company) {
        this.name = name;
        this.idCard = idCard;
        this.qualificationNo = qualificationNo;
        this.company = company;
    }
    
    public byte[] toByteArray() throws UnsupportedEncodingException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        // 姓名(16字节)
        byte[] nameBytes = new byte[16];
        byte[] srcName = name.getBytes("GBK");
        System.arraycopy(srcName, 0, nameBytes, 0, Math.min(srcName.length, 16));
        baos.write(nameBytes, 0, 16);
        
        // 身份证号(18字节)
        byte[] idCardBytes = new byte[18];
        byte[] srcIdCard = idCard.getBytes("GBK");
        System.arraycopy(srcIdCard, 0, idCardBytes, 0, Math.min(srcIdCard.length, 18));
        baos.write(idCardBytes, 0, 18);
        
        // 从业资格证号(20字节)
        byte[] qualBytes = new byte[20];
        byte[] srcQual = qualificationNo.getBytes("GBK");
        System.arraycopy(srcQual, 0, qualBytes, 0, Math.min(srcQual.length, 20));
        baos.write(qualBytes, 0, 20);
        
        // 所属企业(50字节)
        byte[] companyBytes = new byte[50];
        byte[] srcCompany = company.getBytes("GBK");
        System.arraycopy(srcCompany, 0, companyBytes, 0, Math.min(srcCompany.length, 50));
        baos.write(companyBytes, 0, 50);
        
        return baos.toByteArray();
    }
    
    public static DriverInfo fromByteArray(byte[] bytes) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        
        // 姓名(16字节)
        byte[] nameBytes = new byte[16];
        bais.read(nameBytes);
        String name = new String(nameBytes, "GBK").trim();
        
        // 身份证号(18字节)
        byte[] idCardBytes = new byte[18];
        bais.read(idCardBytes);
        String idCard = new String(idCardBytes, "GBK").trim();
        
        // 从业资格证号(20字节)
        byte[] qualBytes = new byte[20];
        bais.read(qualBytes);
        String qualificationNo = new String(qualBytes, "GBK").trim();
        
        // 所属企业(50字节)
        byte[] companyBytes = new byte[50];
        bais.read(companyBytes);
        String company = new String(companyBytes, "GBK").trim();
        
        return new DriverInfo(name, idCard, qualificationNo, company);
    }
    
    // Getter方法
    public String getName() { return name; }
    public String getIdCard() { return idCard; }
    public String getQualificationNo() { return qualificationNo; }
    public String getCompany() { return company; }
    
    @Override
    public String toString() {
        return "DriverInfo{" +
               "name='" + name + '\'' +
               ", idCard='" + idCard + '\'' +
               ", qualificationNo='" + qualificationNo + '\'' +
               ", company='" + company + '\'' +
               '}';
    }
}
